package com.tuyue.police.biz.impl;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.tuyue.police.api.DeviceApi;
import com.tuyue.police.biz.IYsDeviceBiz;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.response.CodeImageResponse;
import com.tuyue.police.util.Result;
import com.tuyue.police.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by dell on 2018/6/1.
 */

@Service
@Slf4j
public class YsDeviceBizImpl implements IYsDeviceBiz {

    @Autowired
    private DeviceApi deviceApi;

    @Override
    public Result addDeviceAndSetAccess(String deviceSerial, String validateCode, String accountId) throws Exception {

        Boolean addDevice = deviceApi.addDevice(deviceSerial, validateCode);
        if (!addDevice) {
            throw new MyException(ResultEnum.YS_DEVICE_DEFEATADD);
        }
        Boolean setAccess = deviceApi.setAccess(accountId, deviceSerial);
        if (!setAccess) {
            throw new MyException(ResultEnum.YS_DEVICE_DEFEATSETACCESS);
        }
        Result result = deviceApi.videoEncryptionOff(accountId, deviceSerial, validateCode);
        System.out.println(result.toString());
        if (result.getCode() != 1) {
            System.out.println("解密失败");
        }

        return ResultUtil.success();
    }

    @Override
    public Result delDeviceAndDelAccess(String deviceSerial, String accountId) {

        Boolean delAccess = deviceApi.delAccess(accountId, deviceSerial);
        if (!delAccess) {
            throw new MyException(ResultEnum.YS_DEVICE_DEFEATDELETEACCESS);
        }
        Boolean delDevice = deviceApi.delDevice(deviceSerial);
        if (!delDevice) {
            throw new MyException(ResultEnum.YS_DEVICE_DEFEATDELETE);
        }
        return ResultUtil.success();
    }

    @Override
    public Result updateDevice(String accountId, String deviceSerial, String deviceName) {

        return deviceApi.updateDevice(accountId, deviceSerial, deviceName);
    }

    @Override
    public Result<CodeImageResponse> qrcode(String accountId, String ssid, String password) {

        return deviceApi.qrcode(accountId, ssid, password);
    }

    @Override
    public Result list(String accountId, int pageStart, int pageSize) {

        return deviceApi.list(accountId, pageStart, pageSize);
    }

    @Override
    public Result getBroadcastAddress(String accountId, String source) {

        return deviceApi.getBroadcastAddress(accountId, source);
    }

    @Override
    public Result policeList(String accountId, Long startTime, Long endTime, Integer alarmType, Integer status, Integer pageStart, Integer pageSize) {

        return deviceApi.policeList(accountId, startTime, endTime, alarmType, status, pageStart, pageSize);
    }

    @Override
    public Result videoEncryptionOn(String accountId, String deviceSerial) {

        return deviceApi.videoEncryptionOn(accountId, deviceSerial);
    }

    @Override
    public Result videoEncryptionOff(String accountId, String deviceSerial, String validateCode) {

        return deviceApi.videoEncryptionOff(accountId, deviceSerial, validateCode);
    }

    @Override
    public Result setProtection(String accountId, String deviceSerial, int isDefence) {

        return deviceApi.setProtection(accountId, deviceSerial, isDefence);
    }

    @Override
    public Result deviceInfo(String accountId, String deviceSerial) {

        return deviceApi.deviceInfo(accountId, deviceSerial);
    }
}
